package com.tian.core.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * @author tianhong yu
 * @date 2018/1/23 13:38
 *  发送短信验证码
 */
public class SendMessageUtil {


    private static  final String product = "Dysmsapi";


    private static  final String domain = "dysmsapi.aliyuncs.com";


    private static  final String accessKeyId = "LTAIQ05YpiogxtVQ";


    private static  final String accessKeySecret = "fSdbUl9SERotpvosH9e22U13PC0HNX";

    /**
     * 发送短信
     */
    public static SendSmsResponse sendMessage(String phoneNumber,String status) throws ClientException{
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();

        //待发送手机号
        request.setPhoneNumbers(phoneNumber);
        //短信签名
        request.setSignName("企业人事管理系统");
        //短信模板
        request.setTemplateCode("SMS_134315630");

        //短信内容
        request.setTemplateParam("{\"status\":\""+status+"\"}");

        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        return sendSmsResponse;
    }
}
